package router

import (
	dto "dhuviewer/dto"
	services "dhuviewer/services"
	"fmt"

	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
)

func UserData(rg *gin.RouterGroup) {
	routerGroup := rg.Group("/")
	routerGroup.GET("/info", func(c *gin.Context) {
		payload, err := HelpcheckJWT(c)
		if err != nil {
			return
		}
		err, userData := services.GetUser(uuid.MustParse(payload.Uid))
		if err != nil {
			HelpError(c)
			return
		} else {
			c.JSON(200, gin.H{
				"status":          20000,
				"invite":          userData.Invite,
				"uid":             userData.Uid,
				"note":            userData.Note.String,
				"gender":          userData.Gender.String,
				"register_date":   userData.RegisterDate.Time,
				"birth_date":      userData.BirthDate.Time,
				"perfered_gender": userData.PerferedGender.String,
			})
			return
		}
	})

	routerGroup.POST("/update", func(c *gin.Context) {
		payload, err := HelpcheckJWT(c)
		if err != nil {
			return
		}
		fmt.Println(payload)
		userInfo := new(dto.User) //注意该结构接受的内容
		c.ShouldBind(&userInfo)
		fmt.Println(payload.Uid)
		err = services.UpdateUser(uuid.MustParse(payload.Uid), userInfo)
		HelpErrorWithCode(c, err)
	})
}
